package cc.lj.internal.coupons.domain.martcap.service.impl;

import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapBatchDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapMerchantDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapBatchDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.convert.CapMerchantDOConvertPO;
import cc.lj.internal.coupons.domain.martcap.repository.ICapMerchantRepository;
import cc.lj.internal.coupons.domain.martcap.repository.IMartCapRepository;
import cc.lj.internal.coupons.domain.martcap.service.ICapBatchDomainService;
import cc.lj.internal.coupons.domain.martcap.service.ICouponsSettlementDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMartCapDomainService;
import cc.lj.internal.coupons.domain.ticket.model.vo.CouponsSettlementDO;
import cc.lj.internal.coupons.infrastructure.po.CapMerchantPO;
import cc.lj.internal.coupons.infrastructure.po.MartCapPO;
import lj.internal.dict.enums.EnumCapBatchIsNewStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 电子券商户封顶额分摊数据查询 domain service
 * @author liangxifeng
 * @date 2022-01-16
 */
@Service("couponsSettlementDomainServiceImpl")
@Slf4j
public class CouponsSettlementDomainServiceImpl implements ICouponsSettlementDomainService {
    //封顶额批次
    @Resource(name = "capBatchDomainServiceImpl")
    private ICapBatchDomainService capBatchDomainService;
    //商户领域层
    @Resource(name = "capMerchantRepository")
    private ICapMerchantRepository capMerchantRepository;
    //封顶额活动领域层
    @Resource(name = "martCapDomainServiceImpl")
    private IMartCapDomainService martCapDomainService;

    @Override
    public void getShareData(CouponsSettlementDO settlementDO) {
        //通过批次主键查询封顶额批次表数据
        CapBatchDO capBatch = new CapBatchDO();
        capBatch.setBatchId(settlementDO.getTicketBatchId());
        List<CapBatchDO> capBatchDOList =  capBatchDomainService.getList(capBatch);

        //如果没有查询到活动批次，则使用原有分摊计算
        if(capBatchDOList.size() ==0) {
            log.info("商户："+settlementDO.getMerchantShow()+",电子券批次主键:"+settlementDO.getTicketBatchId()+",该批次没有在任何封顶额活动中，使用原始分摊计算结算");
            return;
        }

        //组装封顶额活动主键 list
        List<Integer> martCapIdList = new ArrayList<>();
        //定义<封顶额活动主键，CapBatchDO> map
        Map<Integer,CapBatchDO> capBatchDOMap = new HashMap<>();
        capBatchDOList.forEach(capBatchDO -> {
            capBatchDOMap.put(capBatchDO.getCapId(),capBatchDO);
            martCapIdList.add(capBatchDO.getCapId());
        });
        //通过主键 in 查询 封顶额活动表数据
        List<MartCapDO> martCapDOList = martCapDomainService.getByIdIn(martCapIdList);
        if(martCapDOList.size() == 0) {
            log.error("商户："+settlementDO.getMerchantShow()+",电子券批次主键:"+settlementDO.getTicketBatchId()+",通过关联的活动主键id in 查询封顶额活动表数据为空");
        }
        //封顶额活动重新排序，先按着开始时间正序，开始时间相同再按着主键正序
        martCapDOList = martCapDOList.stream().sorted(Comparator.comparing(MartCapDO::getStartDate)
                .thenComparing(Comparator.comparing(MartCapDO::getStartDate)))
                .collect(Collectors.toList());

        //该商户同时参见的多个活动批次的封顶额列表
        List<MartCapBatchDO> martCapBatchDOList = new ArrayList<>();
        //循环排序好的活动list,组合出在活动期内的活动，批次，封顶额数据
        martCapDOList.forEach( martCapDO -> {
            //如果封顶额活动未开始 或活动已结束,则直接返回原始分摊
            if(martCapDO.getIsStart() == 0 || martCapDO.getIsOver()== 1) {
                log.info("商户："+settlementDO.getMerchantShow()+",电子券批次主键:"+settlementDO.getTicketBatchId()+",结算时发现封顶额活动主键:"+martCapDO.getId()+",活动未开始或已结束");
            }else {
                //通过活动主键和展位号查询该商户在该活动的封顶额
                CapMerchantPO capMerchantPO = new CapMerchantPO();
                capMerchantPO.setMerchantShow(settlementDO.getMerchantShow());
                capMerchantPO.setCapId(martCapDO.getId());
                List<CapMerchantPO> list = capMerchantRepository.getList(capMerchantPO);
                if(list.size() == 0) {
                    log.info("商户："+settlementDO.getMerchantShow()+",电子券批次主键:"+settlementDO.getTicketBatchId()+"，结算时发现该商户没有在封顶额活主键:"+martCapDO.getId()+"的商户名单中，使用原始分摊计算结算");
                }else {
                    CapMerchantPO merchantPO = list.get(0);
                    MartCapBatchDO martCapBatchDO = new MartCapBatchDO();
                    martCapBatchDO.setCapId(martCapDO.getId());
                    //通过活动主键，获得活动批次数据
                    CapBatchDO capBatchDO = capBatchDOMap.get(martCapDO.getId());
                    martCapBatchDO.setMartCapRatio(capBatchDO.getMartCapRatio());
                    martCapBatchDO.setCapValue(merchantPO.getMartCapValue());
                    martCapBatchDO.setTicketBatchId(capBatchDO.getBatchId());
                    martCapBatchDO.setMartNewSharePrice(capBatchDO.getMartPrice());
                    martCapBatchDO.setMartNewShareRatio(capBatchDO.getMartShare());
                    martCapBatchDO.setMerchantShow(merchantPO.getMerchantShow());
                    martCapBatchDO.setMerchantNewSharePrice(capBatchDO.getMerchantPrice());
                    martCapBatchDO.setMerchantNewShareRatio(capBatchDO.getMerchantShare());
                    martCapBatchDOList.add(martCapBatchDO);
                }
            }
        });
        settlementDO.setMartCapBatchDTOList(martCapBatchDOList);
    }

}
